package cn.zhz.gen.base;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private Integer code;

    private String msg;

    private T data;

    public static <T> R<T> ok() {
        return ok(null);
    }

    public static <T> R<T> ok(T data) {
        return ok(ResultCode.SUCCESS.message, data);
    }

    public static <T> R<T> ok(String msg, T data) {
        return ok(ResultCode.SUCCESS, msg, data);
    }

    public static <T> R<T> ok(ResultCode resultCode, String msg, T data) {
        return restResult(resultCode.code, msg, data);
    }

    public static <T> R<T> failed() {
        return failed(ResultCode.FAIL.message);
    }

    public static <T> R<T> failed(String msg) {
        return failed(ResultCode.FAIL, msg, null);
    }

    public static <T> R<T> failed(String msg, T data) {
        return failed(ResultCode.FAIL, msg, data);
    }

    public static <T> R<T> failed(ResultCode resultCode, String msg, T data) {
        return restResult(resultCode.code, msg, data);
    }

    public static <T> R<T> error() {
        return error(ResultCode.INTERNAL_SERVER_ERROR.message);
    }

    public static <T> R<T> error(String msg) {
        return restResult(ResultCode.INTERNAL_SERVER_ERROR.code, msg, null);
    }

    public static <T> R<T> custom(Integer code, String msg, T data) {
        return restResult(code, msg, data);
    }


    private static <T> R<T> restResult(Integer code, String msg, T data) {
        return new R<>(code, msg, data);
    }


}
